<template>
	<el-dialog title="试题选择" v-model="visible" width="600px;" destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-aside width="200px" v-loading="showGrouploading">
				<el-container>
					<el-header>
						<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
					</el-header>
					<el-main class="nopadding">
						<el-tree ref="examSort" class="menu" node-key="sortId" :data="examSortList" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false"
								 :filter-node-method="groupFilterNode" @node-click="groupClick">
							<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.code }}</span>
							</span>
							</template>

						</el-tree>
					</el-main>
				</el-container>
			</el-aside>
			<el-container>
				<el-header style="height: auto;">
					<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
				</el-header>
				<el-header>
					<div class="left-panel">
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="search.keyword" placeholder="标题 / 摘要" clearable></el-input>
							<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="apiObj" :params="this.search" row-key="recordId" stripe remoteSort>
						<el-table-column label="#" type="index" width="50"></el-table-column>
						<el-table-column label="试题标题" prop="title" width="200" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="试题分类" prop="sortName" width="100" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="试题类型" prop="examType" width="100">
							<template #default="scope">
								<el-tag v-if="scope.row.examType=='0'">单选</el-tag>
								<el-tag v-if="scope.row.examType=='1'">多选</el-tag>
								<el-tag v-if="scope.row.examType=='2'">简述</el-tag>
							</template>
						</el-table-column>
						<el-table-column label="分值" prop="grade" width="100">
							<template #default="scope">
								{{scope.row.grade}}分
							</template>
						</el-table-column>
						<el-table-column label="答案" prop="answer" width="100"></el-table-column>
						<el-table-column label="备注" prop="remark" width="300" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="操作" fixed="right" align="right" width="100">
							<template #default="scope">
								<el-button-group>
									<el-button text type="primary" size="small" @click="okSelect(scope.row, scope.$index)">选择</el-button>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
			</el-container>
		</el-container>
	</el-dialog>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
export default {
	emits: ['success', 'closed'],
	components: {
		scSelectFilter,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			showGrouploading: false,
			apiObj: this.$API.office.exam.getExamQuestionsListBySortId,
			groupFilterText: '',
			filterData: [{
					title: "试题类型",
					key: "examType",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "单选",
							value: "0"
						},
						{
							label: "多选",
							value: "1"
						},
						{
							label: "简述",
							value: "2"
						}]
				}],
			row:null,
			examSortList: [],
			groupsProps: {
				label: "name",
				value: "sortId"
			},
			search: {
				examType:'',
				sortId:"",
				keyword: ""
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.examSort.filter(val);
		}
	},
	mounted() {
		this.getExamSortTree()
	},
	methods: {
		open(){
			this.visible = true;
			return this
		},
		//加载树数据
		async getExamSortTree() {
			this.showGrouploading = true;
			var res = await this.$API.office.exam.getExamSortTree.get();
			this.showGrouploading = false;
			this.examSortList = res.data;
		},
		filterChange(data){
			this.search.examType = data.examType;
			this.search.sortId = data.sortId;
			this.$refs.table.upData(this.search)
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.name.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			this.sortId = data.sortId
			this.search.sortId = data.sortId
			this.$refs.table.upData(this.search)
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		setData(data)
		{
			this.row = data;
		},
		okSelect(row){
			this.row.recordId=row.recordId;
			this.row.examType=row.examType;
			this.row.title=row.title;
			this.row.grade=row.grade;
			this.row.questionsList = JSON.parse(row.questionsList);
			this.visible = false;
		}
	}
}
</script>
